package com.tbycoder.tby.controller;


import com.tbycoder.tby.config.AccessToken;
import com.tbycoder.tby.entity.Request.CommonIdRequest;
import com.tbycoder.tby.entity.Request.PublishRequest;
import com.tbycoder.tby.entity.Vo.CommonDicVo;
import com.tbycoder.tby.entity.Vo.GetQuestionVo;
import com.tbycoder.tby.excption.BizException;
import com.tbycoder.tby.service.IIposQuestionService;
import com.tbycoder.tby.utils.ApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tby
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/tby/ipos-question")
@CrossOrigin
public class IposQuestionController {

    @Autowired
    IIposQuestionService iIposQuestionService;


    @PostMapping("/getAllQuestion")
    @ApiOperation("获得所有问题和答案")
    public ApiResponse<GetQuestionVo> getAllQuestion() {
        CommonDicVo commonDicVo = new CommonDicVo();
        GetQuestionVo getQuestionVo= iIposQuestionService.getAllQuestion();
        return ApiResponse.success(getQuestionVo);
    }

    @PostMapping("/deleteQuestion")
    @ApiOperation("管理员删除问题和答案")
    public ApiResponse deleteQuestion(
            @RequestBody CommonIdRequest commonIdRequest
    ) {
        boolean success = iIposQuestionService.deleteQuestion(commonIdRequest);
        return ApiResponse.success();
    }
    @PostMapping("/questionAndAnswer")
    @ApiOperation("发表问题和答案")
    public ApiResponse questionAndAnswer(
            @RequestBody PublishRequest publishRequest
    ){
        try {
            iIposQuestionService.questionAndAnswer(publishRequest);
        }catch(BizException e){
            return ApiResponse.error(e.getData());
        }
        return ApiResponse.success();
    }





}
